//求自然月日期
function getMonthBeforeFormatAndDay(num, format, day) {
    var date = new Date();
    console.log(date,'date');
    date.setMonth(date.getMonth() + (num * 1), 1);
    //读取日期自动会减一，所以要加一
    var mo = date.getMonth() + 1;
    //小月
    if (mo == 4 || mo == 6 || mo == 9 || mo == 11) {
        if (day > 30) {
            day = 30
        }
    }
    //2月
    else if (mo == 2) {
        if (isLeapYear(date.getFullYear())) {
            if (day > 29) {
                day = 29
            } else {
                day = 28
            }
        }
        if (day > 28) {
            day = 28
        }
    }
    //大月
    else {
        if (day > 31) {
            day = 31
        }
    }

    retureValue = date.format(('yyyy' + format + 'MM' + format + day),date);

    return retureValue;
}

//JS判断闰年代码
function isLeapYear(Year) {
    if (((Year % 4) == 0) && ((Year % 100) != 0) || ((Year % 400) == 0)) {
        return (true);
    } else { return (false); }
}

//日期格式化
Date.prototype.format = function (format) {
    var o = {
        "M+": date.getMonth() + 1, // month
        "d+": date.getDate(), // day
        "h+": date.getHours(), // hour
        "m+": date.getMinutes(), // minute
        "s+": date.getSeconds(), // second
        "q+": Math.floor((date.getMonth() + 3) / 3), // quarter
        "S": date.getMilliseconds()
        // millisecond
    }

    if (/(y+)/.test(format))
        format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(format))
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}

export { getMonthBeforeFormatAndDay };